import { _decorator, Component, Node, UITransform, Sprite, Animation, SpriteFrame, AnimationClip, animation } from 'cc'
const { ccclass, property } = _decorator

import { EntityManager } from '../../Base/EntityManager'

import { SpkiesManager } from './SpkiesManager'

// 状态
import State from '../../Base/State'

// 状态机类型
import { PLAYER_STATE_TYPE_ENUM, ENTITY_TYPE_ENUM, SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM } from '../../Enums'

// 状态机
import { StateMchine,  getInitParamsNumber } from '../../Base/StateMchine'

import {SpikesOneSubStateMachine} from './SpikesOneSubStateMachine'
import {SpikesTwoSubStateMachine} from './SpikesTwoSubStateMachine'
import {SpikesThreeSubStateMachine} from './SpikesThreeSubStateMachine'
import {SpikesFourSubStateMachine} from './SpikesFourSubStateMachine'

export type pamersValueType = number | string | boolean



@ccclass('SpkiesStateMchine')
export class SpkiesStateMchine extends StateMchine {
  async init() {
    this.animationComponent = this.node.addComponent(Animation)

    this.initPramers() //初始化参数
    this.initStateMachines() //初始化状态机
    this.initAnimationEvent()

    await Promise.all(this.waitingList)
  }

  // 初始化参数
  initPramers() {
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.SPIKES_TOTAL_COUNT, getInitParamsNumber())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.SPIKES_CUR_COUNT, getInitParamsNumber())
  }

  // 初始化状态机
  initStateMachines() {
    this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_ONE,new SpikesOneSubStateMachine(this))
    this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_TWO, new SpikesTwoSubStateMachine(this))
    this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_THREE, new SpikesThreeSubStateMachine(this))
    this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_FOUR, new SpikesFourSubStateMachine(this))
  }

  initAnimationEvent() {
    this.animationComponent.on(Animation.EventType.FINISHED, this.onAnimationEnd, this)
  }

  // 监听动画结束
  onAnimationEnd(event: Animation.EventType, clip: AnimationClip, target: Node) {
    let value = this.getPramers(PLAYER_STATE_TYPE_ENUM.SPIKES_TOTAL_COUNT)
    if((value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_ONE && clip.name.includes('spikesone/two')) ||
     (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_TWO && clip.name.includes('spikestwo/three')) ||
     (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_THREE && clip.name.includes('spikesthree/four')) ||
     (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_FOUR && clip.name.includes('spikesfour/one'))) {
      this.node.getComponent(SpkiesManager).backZero()
    }
  }

  //  定义transition
  run() {
    let value = this.getPramers(PLAYER_STATE_TYPE_ENUM.SPIKES_TOTAL_COUNT)
    switch (this.currentState) {
      case this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE):
      case this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_TWO):
      case this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_THREE):
      case this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_FOUR):
        if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_ONE) {
          this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE) as State
        } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_TWO) {
          this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_TWO) as State
        } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_THREE) {
          this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_THREE) as State
        } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_FOUR) {
          this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_FOUR) as State
        } else {
          this.currentState = this.currentState as State
        }
        break
      default:
        this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE) as State
    }
  }
}
